package com.hemerasolutions.v2trainer.component.table;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.EquipmentDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.table.EquipmentTableCompInterface;
import com.hemerasolutions.v2trainer.model.exercise.Equipment;
import com.hemerasolutions.v2trainer.model.library.Library;
import com.hemerasolutions.v2trainer.service.interfaces.EquipmentServiceInterface;


@Controller
@Component("equipmentTableComp")
@Scope("prototype")
public class EquipmentTableComp extends BaseComponent implements EquipmentTableCompInterface {

	private static final long serialVersionUID = 1L;

	@Autowired
	private EquipmentServiceInterface equipmentService;

	@Autowired
	private EquipmentDetailsCompInterface equipmentDetailsComp;
	
	private List<Equipment> filteredEquipments;
	
	private List<Equipment> foundEquipments;

	private List<Equipment> equipments;
	
	private List<Library> librarys;
	
	private Equipment equipment;

	private String findByEquipmentName;
	
	private String findByEquipmentDescription;

	private String findByEquipmentObs;

	private Date findByBegCreatedAt;
	
	private Date findByEndCreatedAt;
	
	private Date findByBegChangedAt;
	
	private Date findByEndChangedAt;
	
	private boolean findByEquipmentStatus;
	
	private String filterByEquipmentName;
	
	private String filterByEquipmentDescription;

	private String filterByEquipmentObs;

	private Date filterByBegCreatedAt;
	
	private Date filterByEndCreatedAt;
	
	private Date filterByBegChangedAt;
	
	private Date filterByEndChangedAt;		

	public EquipmentTableComp(){
		super();
	}
	
	@Override
	@PostConstruct
	public void init(){
		super.init();
	}
	
	@Override
	public void addEquipment(ActionEvent ev){
		Equipment equipment = (Equipment)ev.getComponent().getAttributes().get("equipment");	

		this.foundEquipments.remove(equipment);
		this.filteredEquipments.add(equipment);
		this.equipments.add(equipment);
	}
	

	@Override
	public void deleteEquipment(ActionEvent ev){
		Equipment equipment = (Equipment)ev.getComponent().getAttributes().get("equipment");	

		this.filteredEquipments.remove(equipment);
		this.equipments.remove(equipment);
		if(this.equipment == equipment){
			this.equipment = null;
		}
	}
	
	@Override
	public void toViewEquipment(){
		this.equipmentDetailsComp.setEquipment(this.equipment);
		this.equipmentDetailsComp.refresh();
	}

	
	@Override
	public void searchEquipmentsInLibrarys(){
		Map<String,Object> fields = new HashMap<String, Object>();
		
			
		if( this.findByBegCreatedAt != null && this.findByEndCreatedAt != null && 
				this.findByBegCreatedAt.compareTo(this.findByEndCreatedAt) > 0){
				this.showErrorMessage(	this.localizedText("CreationDate"),
										this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
				return;
			}
			

		if( this.findByBegChangedAt != null && this.findByEndChangedAt != null && 
			this.findByBegChangedAt.compareTo(this.findByEndChangedAt) > 0){
			this.showErrorMessage(	this.localizedText("ChangeDate"),
									this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
			return;			
		}
    	
		
    	if (this.findByEquipmentName != null && !this.findByEquipmentName.equals("")) {
            fields.put("name", this.findByEquipmentName);
        }
    
    	
    	if (this.findByEquipmentDescription != null && !this.findByEquipmentDescription.equals("")) {
            fields.put("description", this.findByEquipmentDescription);
        }
    	
    	if (this.findByEquipmentObs != null && !this.findByEquipmentObs.equals("")) {
            fields.put("obs", this.findByEquipmentObs);
        }
    	
    	if (this.findByBegCreatedAt != null){
    		fields.put("begCreatedAt", this.findByBegCreatedAt);
    	}
    	if (this.findByEndCreatedAt != null){
            fields.put("endCreatedAt",this.findByEndCreatedAt);
        }
        if (this.findByBegChangedAt != null){
        	fields.put("begChangedAt",this.findByBegChangedAt);
        }
        if (this.findByEndChangedAt != null){
        	fields.put("endChangedAt", this.findByEndChangedAt);
        }

        
       	fields.put("librarys", this.getActiveUserAllowedLibrarys());

	    fields.put("active", this.findByEquipmentStatus);
       	
       	
        this.foundEquipments = this.equipmentService.searchEquipments(fields);
	
		if(!(this.foundEquipments).isEmpty()){
			if(this.filteredEquipments != null){
				for(Equipment equip:this.filteredEquipments){
					if(this.foundEquipments.contains(equip)){
						this.foundEquipments.remove(equip);
					}
				}
			}
		}	

		if((this.foundEquipments).isEmpty()){
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.localizedText("NoEntrysFounded"));
		}else{
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.foundEquipments.size() + " " + 
									this.localizedText("entrysFounded"));
		}			
				
	}

	
	@Override
	public void filterEquipments(){
		Map<String,Object> fields = new HashMap<String, Object>();
		
		this.filteredEquipments = new ArrayList<Equipment>();
		
		if( this.filterByBegCreatedAt != null && this.filterByEndCreatedAt != null && 
				this.filterByBegCreatedAt.compareTo(this.filterByEndCreatedAt) > 0){
				this.showErrorMessage(	this.localizedText("CreationDate"),
										this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
				return;
			}
			

		if( this.filterByBegChangedAt != null && this.filterByEndChangedAt != null && 
			this.filterByBegChangedAt.compareTo(this.filterByEndChangedAt) > 0){
			this.showErrorMessage(	this.localizedText("ChangeDate"),
									this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
			return;			
		}
    	
    	if (this.filterByEquipmentName != null && !this.filterByEquipmentName.equals("")) {
            fields.put("name", this.filterByEquipmentName);
        }

    	if (this.filterByEquipmentDescription != null && !this.filterByEquipmentDescription.equals("")) {
            fields.put("description", this.filterByEquipmentDescription);
        }

    	if (this.filterByEquipmentObs != null && !this.filterByEquipmentObs.equals("")) {
            fields.put("obs", this.filterByEquipmentObs);
        }

    	if (this.filterByBegCreatedAt != null){
    		fields.put("begCreatedAt", this.filterByBegCreatedAt);
    	}
    	if (this.filterByEndCreatedAt != null){
            fields.put("endCreatedAt",this.filterByEndCreatedAt);
        }
        if (this.filterByBegChangedAt != null){
        	fields.put("begChangedAt",this.filterByBegChangedAt);
        }
        if (this.filterByEndChangedAt != null){
        	fields.put("endChangedAt", this.filterByEndChangedAt);
        }

        
        this.filteredEquipments = this.equipmentService.filterEquipments(fields,this.equipments);
		
		if((this.filteredEquipments).isEmpty()){
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.localizedText("NoEntrysFounded"));
		}else{
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.filteredEquipments.size() + " " + 
									this.localizedText("entrysFounded"));
		}			
		
	}
			
	
	@Override
	public List<Equipment> getFoundEquipments() {
		return foundEquipments;
	}

	@Override
	public void setFoundEquipments(List<Equipment> foundEquipments) {
		this.foundEquipments = foundEquipments;
	}

	@Override
	public List<Library> getLibrarys() {
		return librarys;
	}

	@Override
	public void setLibrarys(List<Library> librarys) {
		this.librarys = librarys;
	}

	@Override
	public Equipment getEquipment() {
		return equipment;
	}

	@Override
	public void setEquipment(Equipment equipment) {
		this.equipment = equipment;
	}

	@Override
	public String getFindByEquipmentName() {
		return findByEquipmentName;
	}

	@Override
	public void setFindByEquipmentName(String findByEquipmentName) {
		this.findByEquipmentName = findByEquipmentName;
	}

	@Override
	public String getFindByEquipmentDescription() {
		return findByEquipmentDescription;
	}

	@Override
	public void setFindByEquipmentDescription(String findByEquipmentDescription) {
		this.findByEquipmentDescription = findByEquipmentDescription;
	}

	@Override
	public String getFindByEquipmentObs() {
		return findByEquipmentObs;
	}

	@Override
	public void setFindByEquipmentObs(String findByEquipmentObs) {
		this.findByEquipmentObs = findByEquipmentObs;
	}

	@Override
	public Date getFindByBegCreatedAt() {
		return findByBegCreatedAt;
	}

	@Override
	public void setFindByBegCreatedAt(Date findByBegCreatedAt) {
		this.findByBegCreatedAt = findByBegCreatedAt;
	}

	@Override
	public Date getFindByEndCreatedAt() {
		return findByEndCreatedAt;
	}

	@Override
	public void setFindByEndCreatedAt(Date findByEndCreatedAt) {
		this.findByEndCreatedAt = findByEndCreatedAt;
	}

	@Override
	public Date getFindByBegChangedAt() {
		return findByBegChangedAt;
	}

	@Override
	public void setFindByBegChangedAt(Date findByBegChangedAt) {
		this.findByBegChangedAt = findByBegChangedAt;
	}

	@Override
	public Date getFindByEndChangedAt() {
		return findByEndChangedAt;
	}

	@Override
	public void setFindByEndChangedAt(Date findByEndChangedAt) {
		this.findByEndChangedAt = findByEndChangedAt;
	}

	@Override
	public boolean isFindByEquipmentStatus() {
		return findByEquipmentStatus;
	}

	@Override
	public void setFindByEquipmentStatus(boolean findByEquipmentStatus) {
		this.findByEquipmentStatus = findByEquipmentStatus;
	}

	@Override
	public String getFilterByEquipmentName() {
		return filterByEquipmentName;
	}

	@Override
	public void setFilterByEquipmentName(String filterByEquipmentName) {
		this.filterByEquipmentName = filterByEquipmentName;
	}

	@Override
	public String getFilterByEquipmentDescription() {
		return filterByEquipmentDescription;
	}

	@Override
	public void setFilterByEquipmentDescription(String filterByEquipmentDescription) {
		this.filterByEquipmentDescription = filterByEquipmentDescription;
	}

	@Override
	public String getFilterByEquipmentObs() {
		return filterByEquipmentObs;
	}

	@Override
	public void setFilterByEquipmentObs(String filterByEquipmentObs) {
		this.filterByEquipmentObs = filterByEquipmentObs;
	}

	@Override
	public Date getFilterByBegCreatedAt() {
		return filterByBegCreatedAt;
	}

	@Override
	public void setFilterByBegCreatedAt(Date filterByBegCreatedAt) {
		this.filterByBegCreatedAt = filterByBegCreatedAt;
	}

	@Override
	public Date getFilterByEndCreatedAt() {
		return filterByEndCreatedAt;
	}

	@Override
	public void setFilterByEndCreatedAt(Date filterByEndCreatedAt) {
		this.filterByEndCreatedAt = filterByEndCreatedAt;
	}

	@Override
	public Date getFilterByBegChangedAt() {
		return filterByBegChangedAt;
	}

	@Override
	public void setFilterByBegChangedAt(Date filterByBegChangedAt) {
		this.filterByBegChangedAt = filterByBegChangedAt;
	}

	@Override
	public Date getFilterByEndChangedAt() {
		return filterByEndChangedAt;
	}

	@Override
	public void setFilterByEndChangedAt(Date filterByEndChangedAt) {
		this.filterByEndChangedAt = filterByEndChangedAt;
	}

	@Override
	public List<Equipment> getEquipments() {
		return equipments;
	}

	@Override
	public void setEquipments(List<Equipment> equipments) {
		this.equipments = equipments;
	}

	@Override
	public void refresh() {
		this.equipment = null;
		this.filteredEquipments = new ArrayList<Equipment>();
		this.filteredEquipments.addAll(this.equipments);
	}

	@Override
	public List<Equipment> getFilteredEquipments() {
		return filteredEquipments;
	}

	@Override
	public void setFilteredEquipments(List<Equipment> filteredEquipments) {
		this.filteredEquipments = filteredEquipments;
	}


	@Override
	public EquipmentDetailsCompInterface getEquipmentDetailsComp() {
		return equipmentDetailsComp;
	}

	@Override
	public void setEquipmentDetailsComp(
			EquipmentDetailsCompInterface equipmentDetailsComp) {
		this.equipmentDetailsComp = equipmentDetailsComp;
	}

	
}
